import { dialog } from 'electron'

// 打开选择pdf 窗口
export const openDialog = async (): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openFile'],
    filters: [{ name: 'PDF', extensions: ['pdf'] }],
  })
  return result
}
// 打开特定文件
export const openDialogType = async (fileType = ''): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openFile'],
    filters: [{ name: fileType, extensions: [fileType] }],
  })
  return result
}
export const openDialogDoc = async (): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openFile'],
    filters: [{ name: 'word', extensions: ['doc', 'docx'] }],
  })
  return result
}
// 打开选择.zbd .zbdjm
export const openDialogZbdFile = async (extensions: string): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openFile'],
    filters: [{ name: 'ZBD', extensions: [extensions] }],
  })
  return result
}
// 自定后缀 ['zbd'], ['zbdjm']
export const openDialogCustomSuffix = async (suffixArr: string[]): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openFile'],
    filters: [{ name: 'ZBD', extensions: suffixArr }],
  })
  return result
}
// 打开选择文件夹
export const openDialogDir = async (): Promise<any> => {
  const result = await dialog.showOpenDialogSync({
    properties: ['openDirectory'],
  })
  return result
}

// 通用方法
/**
 * 从url获取文件名
 * @param pathUrl
 */
export const getFileName = (pathUrl: string): string => {
  const startIndex = pathUrl.lastIndexOf('/') + 1
  return pathUrl.substring(startIndex)
}
